package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.RegionInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.RegionInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.RegionInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.RegionInfoBean;
import cc.rengu.igas.bomp.facade.request.RegionInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.RegionInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zhangxuran
 * @Date: 2020/4/6 23:49
 * @Description: 行政区划代码查询
 */
public class RegionInfoQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        RegionInfoQueryRequest queryRequest = new RegionInfoQueryRequest();
        ConvertUtil.convertOutput(queryRequest);
        if (queryRequest.getPageIndex() < 0 || queryRequest.getPageSize() < 0) {
            if (queryRequest.getPageIndex() != -1 && queryRequest.getPageSize() != -1) {
                rglog.error("行政区划代码查询，页数页码不符合规范！");
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        }

        if (queryRequest.getPageSize() > 100) {
            rglog.error("行政区划代码查询，查询页数超过100条不符合规范！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        return queryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        RegionInfoQueryRequest queryRequest = (RegionInfoQueryRequest) request;
        BizResponse<RegionInfoQueryResponse> bizResponse = new BizResponse<>();
        RegionInfoQueryResponse queryResponse = new RegionInfoQueryResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);

        int pageIndex = queryRequest.getPageIndex();
        int pageSize = queryRequest.getPageSize();
        RegionInfoMapper regionInfoMapper = new RegionInfoMapperImpl();
        List<RegionInfo> regionInfoList = new ArrayList<>();
        if (-1 == pageIndex || -1 == pageSize) {
            regionInfoList = regionInfoMapper.selectRegionInfoListByCondition(queryRequest.getSuperRegionCode(),
                    queryRequest.getRegionLevel(), queryRequest.getRegionName());
            if (CollectionUtils.isEmpty(regionInfoList)) {
                rglog.error("根据查询行政区划代码 pageResult 为null !");
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                queryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                queryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setResult(queryResponse);
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }

        } else {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(pageIndex);
            pageInfo.setPageSize(pageSize);

            PageResult pageResult = regionInfoMapper.selectRegionInfoPageListByCondition(queryRequest.getSuperRegionCode(),
                    queryRequest.getRegionLevel(), queryRequest.getRegionName(), pageInfo);

            if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                rglog.info("根据查询条件查询到0条行政区划代码!");
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                queryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                queryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                bizResponse.setResult(queryResponse);
                return bizResponse;
            }

            /* 根据取出记录条数做对应处理 */
            if (pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条行政区划代码!");
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                queryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                queryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                bizResponse.setResult(queryResponse);
                return bizResponse;
            } else {
                for (Object object : pageResult.getResult()) {
                    regionInfoList.add((RegionInfo) object);
                }

                rglog.info("SIZE=<{}>", pageResult.getResult().size());

                queryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
                queryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
            }

        }

        if (!CollectionUtils.isEmpty(regionInfoList)) {
            /* 根据分页查询条件截取相关数据记录 */
            List<RegionInfoBean> regionInfoBeanList = new ArrayList<>();
            regionInfoList.forEach(item -> convertRegionInfoBean(item, regionInfoBeanList));

            queryResponse.setRegionInfoList(regionInfoBeanList);
        } else {
            queryResponse.setTotalRows(0);
        }

        queryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(queryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        RegionInfoQueryResponse queryResponse = (RegionInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(queryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param regionInfo     数据库表结构对象
     * @param regionInfoList 接口对象
     */
    private void convertRegionInfoBean(RegionInfo regionInfo, List<RegionInfoBean> regionInfoList) {
        RegionInfoBean regionInfoBean = new RegionInfoBean();
        BeanUtil.beanCopy(regionInfo, regionInfoBean);
        regionInfoList.add(regionInfoBean);
    }
}
